package com.atguigu.juc.sync;

/**
 * @since 2021-07-15 0:17
 */
//步骤一.创建资源类,创建属性和操作方法
class Ticket {
    private Integer number = 30;

    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "--卖出第" + number-- + "张票,还剩: " + number);
        }
    }
}

public class SaleTicket {
    public static void main(String[] args) {
        //步骤二,创建多线程调用资源类的方法
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "AAA").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "BBB").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "CCC").start();
    }
}
